layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var $ = layui.jquery;

    $(function () {
        // validateKickout();
        $('.imgcode').click(function () {
            var url = ctx + "captcha/captchaImage?s=" + Math.random();
            $(".imgcode").attr("src", url);
        });
    });


    /*function validateKickout() {
        if (getParam("kickout") == 1) {
            layer.alert("<font color='red'>您已在别处登录，请您修改密码或重新登录</font>", {
                    icon: 0,
                    title: "系统提示"
                },
                function(index) {
                    //关闭弹窗
                    layer.close(index);
                     if (top != self) {
                        top.location = self.location;
                    } else {
                        var url  =  location.search;
                        if (url) {
                            var oldUrl  = window.location.href;
                            var newUrl  = oldUrl.substring(0,  oldUrl.indexOf('?'));
                            self.location  = newUrl;
                        }
                    }
                });
        }
    }*/

    //自定义表单验证
    form.verify({
        username: function (value) {
            if (value.length < 5) {
                return '用户名至少得5个字符啊';
            }
        }
        , password: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ]
    });

    //登录按钮
    form.on("submit(login)", function (data) {
        $('#loginSubmit').text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        var username = $("#username").val().trim();
        var password = $("#password").val().trim();
        var validateCode = $("#validateCode").val();
        var rememberMe = $("#rememberme").is(':checked');
        $.ajax({
            type: "post",
            url: "login",
            data: {
                "username": username,
                "password": password,
                "validateCode": validateCode,
                "rememberMe": rememberMe
            },
            success: function (data) {
                if (data.code == 0) {
                    location.href = ctx + 'index';
                } else {
                    layer.msg(data.msg, {time: 2000});
                    $('#loginSubmit').text("登录").attr("disabled", false).removeClass("layui-disabled");
                    $('.imgcode').click();
                    $("#validateCode").val("");

                }
            }
        });


        return false;
    });

    //表单输入框聚焦效果
    $(".loginBody .input-item").click(function (e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    });
});


